package com.molyfun.parents.activity.setting;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class WebViewActivity extends BaseActivity implements View.OnClickListener{

    public static final String WEBVIEW_TITLE_FLAG = "webview_title";

    public static final String WEBVIEW_URL_FLAG = "webview_url";

    @InjectView(R.id.activity_webview)
    WebView mWebView;
    @InjectView(R.id.title)
    TextView mTitle;
    @InjectView(R.id.activity_webview_progress)
    ProgressBar mProgressBar;
    @InjectView(R.id.base_title_left)
    ImageView mBack;

    private String mTitleStr;
    private String mUrlStr;

    public static void startActivity(Context context, String url, String title){
        Intent intent = new Intent();
        intent.setClass(context, WebViewActivity.class);
        intent.putExtra(WEBVIEW_TITLE_FLAG, title);
        intent.putExtra(WEBVIEW_URL_FLAG, url);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");

        Intent intent = getIntent();
        if(intent != null){
            mUrlStr = intent.getStringExtra(WEBVIEW_URL_FLAG);
            mTitleStr = intent.getStringExtra(WEBVIEW_TITLE_FLAG);
            if (!TextUtils.isEmpty(mTitleStr)) {
                mTitle.setText(mTitleStr);
            }
            if (!TextUtils.isEmpty(mUrlStr)) {
                initWebView();
                mWebView.loadUrl(mUrlStr);
            }
        }
        mBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    protected void initWebView() {
        // 初始化网页设置
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);			  // 正常加载缓存
        webSettings.setJavaScriptEnabled(true);						 // 支持JavaScript
        webSettings.setSupportZoom(true);							  // 不支持网页缩放
//        webSettings.setBuiltInZoomControls(true);
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);  // 滚动条悬浮在网页上，不占页面宽度
        mWebView.setOnCreateContextMenuListener(this);
        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                mProgressBar.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
            }
            public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed() ;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                mProgressBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!url.equals("about:blank")) {
                    mWebView.loadUrl(url);
                }
                return false;
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                mProgressBar.setProgress(newProgress);
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     final JsResult result) {
                AlertDialog.Builder b = new AlertDialog.Builder(WebViewActivity.this);
                b.setTitle(R.string.app_name);
                b.setMessage(message);
                b.setPositiveButton(android.R.string.ok,
                        new AlertDialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.confirm();
                            }
                        });
                b.setCancelable(false);
                b.create();
                b.show();
                return true;
            };

            @Override
            public boolean onJsConfirm(WebView view, String url, String message,
                                       final JsResult result) {
                AlertDialog.Builder b = new AlertDialog.Builder(WebViewActivity.this);
                b.setTitle(R.string.app_name);
                b.setMessage(message);
                b.setPositiveButton(android.R.string.ok,
                        new AlertDialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.confirm();
                            }
                        });
                b.setNegativeButton(android.R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.cancel();
                            }
                        });
                b.setCancelable(false);
                b.create();
                b.show();
                return true;
            };

            @Override
            public boolean onJsPrompt(WebView view, String url, String message,
                                      String defaultValue, final JsPromptResult result) {

                return true;
            };
        });

    }

    @Override
    protected void onDestroy() {
        mWebView.stopLoading();
        mWebView.destroy();
        mWebView = null;
        super.onDestroy();
    }

}
